<template>
  <div class="loginForm" :style="{ width: isShow ? '60%' : '90%' }">
    <h3 class="title">{{ $t("login.logIn") }}</h3>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <el-form-item :label="$t('login.username')" prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          :placeholder="`请输入` + $t('login.username')"
          clearable
        >
          <svg-icon
            slot="prefix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item :label="$t('login.password')" prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          :placeholder="`请输入` + $t('login.password')"
          @keyup.enter.native="handleLogin"
          clearable
          show-password
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-checkbox
        v-model="loginForm.rememberMe"
        style="margin: 10px 0px 25px 0px"
        >{{ $t("login.remember") }}</el-checkbox
      >
      <el-form-item style="width: 100%; margintop: 10px">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">{{ $t("login.logIn") }}</span>
          <span v-else>{{ $t("login.loginIng") }}</span>
        </el-button>
      </el-form-item>
    </el-form>
    <el-divider content-position="center">{{ $t("login.linkMe") }}</el-divider>
    <div class="icon_list">
      <i class="iconfont icon-qq"></i>
      <i class="iconfont icon-dianhua"></i>
      <i class="iconfont icon-weixin"></i>
      <i class="iconfont icon-daohang" @click="gotoUrl"></i>
    </div>
  </div>
</template>

<script>
import LangSelect from "@/components/LangSelect";
import { getCodeImg } from "@/api/login";
import { encrypt, decrypt } from "@/utils/jsencrypt";
export default {
  props: {
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  components: { LangSelect },
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false, // 验证码开关
      captchaOnOff: false, // 注册开关
      register: true,
      redirect: undefined,
    };
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = sessionStorage.getItem("username");
      const password = sessionStorage.getItem("password");
      const rememberMe = sessionStorage.getItem("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            sessionStorage.setItem("username", this.loginForm.username);
            sessionStorage.setItem(
              "password",
              encrypt(this.loginForm.password)
            );
            sessionStorage.setItem("rememberMe", this.loginForm.rememberMe);
          } else {
            sessionStorage.removeItem("username");
            sessionStorage.removeItem("password");
            sessionStorage.removeItem("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              if (this.loginForm.username == 888) {
                this.$router.push({
                  name:'PadCheck'
                });
              } else {
                sessionStorage.setItem("account",this.loginForm.username)
                this.$router
                  .push({ path: this.redirect || "/" })
                  .catch(() => {});
              }
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
    gotoUrl() {
      window.open("http://www.jxtykj.cn/");
    },
  },
};
</script>

<style lang="less" scoped>
.loginForm {
  width: 60%;
}
/deep/ .el-form-item__label::before {
  content: "" !important;
}
/deep/ .el-form-item__label {
  line-height: 45px;
}
/deep/ .el-input--medium .el-input__inner {
  height: 40px;
}
.icon_list {
  display: flex;
  //   width: 500px;
  justify-content: space-around;
  .iconfont {
    font-size: 30px;
    text-align: center;
    color: #999999;
    cursor: pointer;
  }
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../../../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 10px auto;
  text-align: center;
  font-size: 26px;
  font-weight: bold;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 100%;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
.login .el-dropdown {
  display: inline-block;
  position: absolute;
  color: #606266;
  font-size: 20px;
  top: 0px;
  right: 50px;
}
</style>